package cz.zcu.fav.ratdroid.activity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import cz.zcu.fav.ratdroid.R;
import cz.zcu.fav.ratdroid.activity.calendar.reservation.ReservationFactory;
import cz.zcu.fav.ratdroid.adapter.NotReturnedArrayAdapter;
import cz.zcu.fav.ratdroid.bean.Period;
import cz.zcu.fav.ratdroid.bean.Reservations;
import cz.zcu.fav.ratdroid.service.IRestListener;
import cz.zcu.fav.ratdroid.service.RestService;

/***
 * Activity shows list of not-returned items and on selection click violates
 * return action
 * 
 * @author Lukas Gemela
 * 
 */
public class ReturnItBackActivity extends
		SkeletonRestListActivity<Reservations>
{

	private NotReturnedArrayAdapter adapter;

	private List<Period> loadedObjects = new ArrayList<Period>();
	private static String LOADED_UNRETURNED_KEY = "loaded.unreturned";

	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.object_browser_layout);
		if (savedInstanceState != null)
		{
			loadedObjects = (List<Period>) savedInstanceState
					.getSerializable(LOADED_UNRETURNED_KEY);
		} else
		{
			//load data from server
			loadedObjects = provideData();
		}

		adapter = new NotReturnedArrayAdapter(this, R.layout.object_row,
				loadedObjects);
		setListAdapter(adapter);

		getListView().setOnItemClickListener(new OnItemClickListener()
		{

			///violate return action to on item click
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id)
			{

				Period period = getLoadedObjects().get(position);
				setRestTask(RestService.getInstance().returnObject(
						period.getId(), ReturnItBackActivity.this,
						new IRestListener<Reservations>()
						{
							//if return action succeeded return back to main screen
							public void notifyDataLoaded(Reservations data)
							{
								finish();
							}

							public void notifyException(final Exception ex)
							{
								runOnUiThread(new Runnable()
								{
									//show error
									public void run()
									{
										Toast.makeText(
												getApplicationContext(),
												"Error eccured: "
														+ ex.getMessage(),
												Toast.LENGTH_LONG).show();
									}
								});
							}

						}));
			}
		});

	}

	public List<Period> getLoadedObjects()
	{
		return loadedObjects;
	}

	@Override
	protected void onSaveInstanceState(Bundle outState)
	{
		super.onSaveInstanceState(outState);
		outState.putSerializable(LOADED_UNRETURNED_KEY,
				(Serializable) loadedObjects);
	}

	//return empty list for now and initiate async comm task
	private List<Period> provideData()
	{
		setRestTask(RestService.getInstance().getNotreturnedObjects(this, this));
		return new ArrayList<Period>();
	}

	public void notifyDataLoaded(Reservations data)
	{
		super.notifyDataLoaded(data);
		if (data != null)
		{
			List<Period> periods = ReservationFactory.getAsPeriodList(data);

			//add all loaded items to adapter
			adapter.notifyDataSetChanged();
			if (data != null)
			{
				adapter.clear();
				adapter.addAll(periods);
				adapter.notifyDataSetChanged();
			}
		}
	}
}
